#pragma once
#include <string>
#include <vector>
#include <fcntl.h>

void SetNonBlock(int sock)
{
    int f1 = fcntl(sock, F_GETFL);
    if(f1 < 0)
    {
        std::cout << "fcntl error" << std::endl;
        return;
    }
    fcntl(sock, F_SETFL, f1 | O_NONBLOCK);
}

//1+2X3+5X6+7
void SplitSegment(std::vector<std::string>& ret, std::string& buffer, const std::string& sep)
{
    while(true)
    {
        auto pos = buffer.find(sep, 0);
        if(pos == std::string::npos)
            break;
        ret.push_back(buffer.substr(0, pos));
        buffer.erase(0, pos+sep.size());
    }
}

bool Deserialize(std::string& data1, std::string& data2, const std::string& buffer)
{
    std::string sep = "+";
    auto pos = buffer.find(sep, 0);
    if(pos == std::string::npos)
        return false;
    data1 = buffer.substr(0, pos);
    data2 = buffer.substr(pos+sep.size());
    return true;
}